package com.hfstu.framework.core.orm.mybatis;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import com.hfstu.framework.model.BakDeleteModel;
import com.hfstu.framework.model.BaseModel;
import com.hfstu.framework.uuid.UUIDGenerator;

public class MyBatisDao<T extends BaseModel, ID extends Serializable> extends
		SqlSessionDaoSupport {

	public void save(String key, T t) {
		t.setId(UUIDGenerator.getUUID());
		t.setCreateTime(new Date());
		getSqlSession().insert(key, t);
	}

	public T get(String key, ID id) {
		return getSqlSession().selectOne(key, id);
	}

	public void delete(String key, ID id) {
		T t = get(key, id);
		getSqlSession().delete(key, t);
	}

	public void delete(String key, T t) {
		if (t instanceof BakDeleteModel) {
			((BakDeleteModel) t).setBeenDeleted(1);
			((BakDeleteModel) t).setDeletedTime(new Date());
			getSqlSession().update(key, t);
		} else {
			getSqlSession().delete(key, t);
		}

	}

	public List<T> queryList(String key, Object obj) {
		return getSqlSession().selectList(key, obj);
	}
}
